//
//  VVNavigationViewController.swift
//  Weibo
//
//  Created by 王惠平 on 2017/3/26.
//  Copyright © 2017年 Will Wang. All rights reserved.
//

import UIKit

class VVNavigationViewController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        //设置边缘手势代理
        self.interactivePopGestureRecognizer?.delegate = self
    }
    
    //监听push操作
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
    
        if self.viewControllers.count > 0 {
            
            //如果当前的控制器的个数等于1 ,表示根视图控制器
            if self.viewControllers.count == 1 {
                
                //获取上一级控制器的标题
                let title = self.viewControllers.first?.title
                
                //设置push进来控制器左侧按钮
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title!, imageName: "navigationbar_back_withtext", target: self, action: #selector(leftAction))
                
                viewController.navigationItem.title = "当前显示的是第\(self.viewControllers.count + 1)级控制器"
            
            } else {
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(leftAction))
                
                viewController.navigationItem.title = "当前显示的是第\(self.viewControllers.count + 1)级控制器"
                
            }
            
            //隐藏tabbar会有黑影
            //解决,1.设置window的背景色
            //2.设置tabbar视图的背景图片
            viewController.hidesBottomBarWhenPushed = true
        }

        //print("当前控制器的个数\(self.viewControllers.count)")
        super.pushViewController(viewController, animated: animated)
        //print("当前控制器的个数\(self.viewControllers.count)")
        
    }
    
    @objc private func leftAction() {
        
        self.popViewController(animated: true)
        
    }
    
    //边缘手势的代理方法
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        //如果是根视图控制器不需要开启边缘手势
        if self.viewControllers.count == 1 {
            
            return false
        }
        
        return true
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
